/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function BVMenu(oElement)
{
	this.m_oElement = oElement;
	this.m_oPrevious = null;
}

BVMenu.prototype.init = function()
{
	var i;
	var aMenuItems;
	var oTemp;

	var aMenuItems = ["spnMenuBarProfile",
					  "spnMenuBarTesting",
					  "spnMenuBarReporting"];
	for (i = 0;
		 i < aMenuItems.length;
		 ++i)
	{
		oTemp = $get(aMenuItems[i]);
		oTemp.attachEvent("onclick", $createDelegate(this, this.expand));
		document.body.attachEvent("onclick", $createDelegate(this, this.collapse));
		// oTemp.attachEvent("onmouseout", $createDelegate(this, this.collapse));
	}

	aMenuItems = ["liNewProfile",
				  "liOpenProfile",
				  "liEditProfile",
				  "liNewScript",
				  "liOpenScript",
				  "liSaveScript",
				  "liReplay",
				  "liTestScreenshot",
				  "liViewReport",
				  "liHistoricalReport"];

	for (i = 0;
		 i < aMenuItems.length;
		 ++i)
	{
		oTemp = $get(aMenuItems[i]);
		oTemp.attachEvent("onmouseover", $createDelegate(this, this.highlight));
		oTemp.attachEvent("onmouseout", $createDelegate(this, this.unhighlight));
		oTemp.attachEvent("onclick", $createDelegate(this, this.menuAction));
	}

	document.body.attachEvent("onclick",
							  $createDelegate(window.oActionTree, window.oActionTree.hideContextMenu));
	
};


BVMenu.prototype.expand = function()
{
	var oElem = window.event.srcElement;

	if (null != this.m_oPrevious)
	{
		this.m_oPrevious.childNodes[1].style.display = "none";	
		// oElem.childNodes[1].style.zIndex = "-1";
		this.m_oPrevious.style.color = "black";		
	}
	
	if ("true" == oElem.toplevel &&
		this.m_oPrevious !== oElem)
	{
		oElem.childNodes[1].style.display = "block";
		// oElem.childNodes[1].style.zIndex = "10";
		oElem.style.color = "white";
		this.m_oPrevious = oElem;
	}
	else if (this.m_oPrevious === oElem)
	{
		this.m_oPrevious = null;
	}
	
	window.event.cancelBubble = true;
	
};


BVMenu.prototype.collapse = function()
{
	
	if (null != this.m_oPrevious)
	{
		this.m_oPrevious.childNodes[1].style.display = "none";	
		// oElem.childNodes[1].style.zIndex = "-1";
		this.m_oPrevious.style.color = "black";		
		this.m_oPrevious = null;
	}
	return;
	



	var oElem = window.event.srcElement;

	if ("true" == oElem.toplevel)
	{
		oElem.childNodes[1].style.display = "none";	
		// oElem.childNodes[1].style.zIndex = "-1";
		oElem.style.color = "black";
	}
	
};


BVMenu.prototype.highlight = function()
{
	var oEvent = window.event;
	var oElem = event.srcElement;

	oElem.style.backgroundColor = "#01457A";
	oElem.style.color = "white";
};


BVMenu.prototype.unhighlight = function()
{
	var oEvent = window.event;
	var oElem = event.srcElement;

	oElem.style.backgroundColor = "#ACC5D6";
	oElem.style.color = "black";
};


BVMenu.prototype.menuAction = function()
{
	var oElem = window.event.srcElement;

	switch (oElem.id)
	{

		case "liNewProfile":
			this.newProfile();
			break;

		case "liOpenProfile":
			this.openProfile();
			break;

		case "liEditProfile":
			this.editProfile(null);
			break;

		case "liNewScript":
			this.newScript();
			break;

		case "liOpenScript":
			this.openScript();
			break;

		case "liSaveScript":
			this.saveScript();
			break;

		case "liReplay":
			this.replay();
			break;

		case "liTestScreenshot":
			window.oScreenshots.test();
			break;

		case "liViewReport":
			window.oReport.unhide();
			break;

		case "liHistoricalReport":
			window.oReport.showHistoricalReport();
			break;
	}
	
};


BVMenu.prototype.newProfile = function()
{
	this.collapse();
	var oProfile = {"name" : null,
					"defaultUrl" : null,
					"emailOnlyOnFail" : null};
	window.showModalDialog("dialogs/createprofile.php",
						   oProfile,
						   "dialogHeight: 200px;" +
						   "dialogWidth: 350px;" + 
						   "center: yes;" +
						   "edge: raised;" +
						   "scroll: no;" +
						   "status: no;" +
						   "unadorned: yes;");
	if (null != oProfile.name)
	{
		window.oProfiles.createProfile(oProfile.name,
									   oProfile.defaultUrl,
									   oProfile.emailOnlyOnFail);
	}
};


BVMenu.prototype.openProfile = function()
{
	this.collapse();
	var oProfile = {"guid" : null};
	
	window.showModalDialog("dialogs/OpenProfile.php",
						   oProfile,
						   "dialogHeight: 275px;" +
						   "dialogWidth: 350px;" + 
						   "center: yes;" +
						   "edge: raised;" +
						   "scroll: no;" +
						   "status: no;" +
						   "unadorned: yes;");	
	if (null != oProfile.guid)
	{
		window.oProfiles.open(oProfile.guid);
	}
		
};


BVMenu.prototype.editProfile = function(sProfile)
{
	this.collapse();
	var oEditProfile = {"name" : null,
						"defaultUrl" : null,
						"emailOnlyOnFail" : null,
						"guid" : null};
	window.showModalDialog("dialogs/EditProfile.php",
						   oEditProfile,
						   "dialogHeight: 400px;" +
						   "dialogWidth: 350px;" + 
						   "center: yes;" +
						   "edge: raised;" +
						   "scroll: no;" +
						   "status: no;" +
						   "unadorned: yes;");		
	if (null != oEditProfile)
	{
		window.oProfiles.edit(oEditProfile);
	}
		
};


BVMenu.prototype.newScript = function()
{
	this.collapse();
	window.oAppEvents.ClearEvents();
};


BVMenu.prototype.openScript = function()
{
	this.collapse();
	var oFileName = {"name":null};
	window.showModalDialog("dialogs/files.php",
						   oFileName,
						   "dialogHeight: 300px;" +
						   "dialogWidth: 350px;" + 
						   "center: yes;" +
						   "edge: raised;" +
						   "scroll: no;" +
						   "status: no;" +
						   "unadorned: yes;");
	if (null != oFileName.name)
	{
		window.oAppEvents.OpenScript(oFileName.name);
	}
};


BVMenu.prototype.saveScript = function()
{
	var oFileName = {"name":null};
	var oHttp = null;

	this.collapse();
	
	window.showModalDialog("dialogs/saveFile.php",
						   oFileName,
						   "dialogHeight: 300px;" +
						   "dialogWidth: 350px;" + 
						   "center: yes;" +
						   "edge: raised;" +
						   "scroll: no;" +
						   "status: no;" +
						   "unadorned: yes;");

	if (null != oFileName.name)
	{
		window.oAppEvents.SaveScript(oFileName.name);
	}
};


BVMenu.prototype.replay = function()
{
	this.collapse();
	window.oReplay.replay();
};
	

	




